/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Ling
 */
public class F_updatecustomer {
    private int idcustomer=0;
    private String c_name;

    public void setC_name(String c_name) {
        this.c_name = c_name;
    }
    
    public String getC_name() {
        return c_name;
    }


    private Connection conn;
    private String SQLStatement;
    private String URL;
    private Statement stmt;
    public ResultSet rset;

    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void getcustomer(int customerid) {
        connect();
        try {
           SQLStatement = "SELECT * from customer WHERE idcustomer =" +customerid;
           rset = stmt.executeQuery(SQLStatement);
           if(rset.next()){
               idcustomer = rset.getInt(1);
               c_name = rset.getString(2);
           }
           rset.close();
           disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
    
     public void updatecustomer(){
        System.out.println("update customer"+c_name+" "+idcustomer);
        try{
           SQLStatement = "UPDATE customer SET c_name = '" +c_name +"' WHERE idcustomer =" +idcustomer;
           stmt.executeUpdate(SQLStatement);
           disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
}
